import { createStore } from 'vuex'
import user from '@/store/modules/user'
import cart from '@/store/modules/cart'
import category from '@/store/modules/category'

import createPersistedState from 'vuex-persistedstate'
import { orderStatus } from '@/constant'
// A模块, A模块中没有加命名空间, state区分模块,
// 其他 getters、mutations、actions 都在全局
const moduleA = {
  state: {
    hello: 'moduleA'
  },
  getters: {
    newName(state) {
      return state.hello + '!!!'
    }
  },
  mutations: {
    updateName(state) {
      state.hello = `moduleAAAAA`
    }
  }
}
// B模块, B模块都有命名空间, 所有功能区分模块, 更高封装度和复用
const moduleB = {
  namespaced: true,
  state: {
    world: 'moduleB'
  },
  getters: {
    newName(state) {
      return state.world + '!!!'
    }
  },
  mutations: {
    updateName(state) {
      state.world = `moduleBBBB`
    }
  }
}

// vue2.0 创建仓库 new Vuex.Store({})
// vue3.0 创建仓库 createStore({})
export default createStore({
  state: {
    username: 'llhuang10',
    orderId: 0
  },
  getters: {
    getNewName: (state) => {
      return state.username + '!!!'
    }
  },
  mutations: {
    updateOrderId(state) {
      debugger
      state.orderId++
    }
  },
  actions: {},
  modules: {
    user,
    cart,
    category
  },
  // 配置插件
  plugins: [
    // 默认存储在localStorage
    createPersistedState({
      // 本地存储名字
      key: 'erabbit-client-pc-store',
      // 指定需要存储的模块
      paths: ['user', 'cart']
    })
  ]
})
